package com.central.apps.handler.inspcriterion;

import com.central.app.service.HandlerService;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController

public class InsptaskHandler {
    @Autowired
    private HandlerService handlerService;
    @PostMapping("/repairOrder")
    public  String repair(@RequestBody Map<String,Object> params)throws Exception{
        JSONObject json = new JSONObject();
        json.put("code", "0");
        String id = MapUtils.getString(params, "id");
        //查询提报信息
        List<Map<String, Object>> resultlist = handlerService.findBySql("SELECT a.orgcode AS orgcode,a.orgdesc AS orgdesc,a.instcheckpost AS instcheckpost,a.status AS STATUS,a.sitecode AS sitecode,a.sitecode AS sitecode,a.sitedesc AS sitedesc,a.deptcode AS deptcode,a.deptdesc AS deptdesc,b.insaenum AS insaenum,b.insaedesc AS insaedesc,c.assetnum AS assetnum,c.assetdesc AS assetdesc,d.abnormaldesc AS abnormaldesc,d.inspperdesc AS inspperdesc,d.createtime AS createtime\n" +
                "                            FROM gq_insptask a \n" +
                "                           LEFT JOIN gq_insptarea b ON a.insptasknum = b.insptasknum\n" +
                "                            LEFT JOIN gq_insptstand c ON b.id = c.insptareaid\n" +
                "                            LEFT JOIN gq_instaskcont d ON c.id = d.insptstandid\n" +
                "                            WHERE d.id='" + id + "'");
        if (CollectionUtils.isNotEmpty(resultlist)){
            //获取提交信息
            Map<String, Object> resultMap = resultlist.get(0);

            String idt = MapUtils.getString(resultMap,"id");
            String orgcode = MapUtils.getString(resultMap, "orgcode");
            String orgdesc = MapUtils.getString(resultMap, "orgdesc");
            String instcheckpost = MapUtils.getString(resultMap, "instcheckpost");
            String status = MapUtils.getString(resultMap, "STATUS");
            String sitecode = MapUtils.getString(resultMap, "sitecode");
            String sitedesc = MapUtils.getString(resultMap, "sitedesc");
            String deptcode = MapUtils.getString(resultMap, "deptcode");
            String deptdesc = MapUtils.getString(resultMap, "deptdesc");
            String insaenum = MapUtils.getString(resultMap, "insaenum");
            String insaedesc = MapUtils.getString(resultMap, "insaedesc");
            String assetnum = MapUtils.getString(resultMap,"assetnum");
            String assetdesc = MapUtils.getString(resultMap, "assetdesc");
            String abnormaldesc = MapUtils.getString(resultMap,"abnormaldesc");
            String  inspperdesc= MapUtils.getString(resultMap,"inspperdesc");
            String createtime = MapUtils.getString(resultMap,"createtime");

            // 组织工单信息
            Map<String, Object> map = new HashMap<>();
            map.put("description", abnormaldesc);
            map.put("status",status);
            map.put("orgcode",orgcode);
            map.put("orgdesc",orgdesc);
            map.put("sitecode",sitecode);
            map.put("sitedesc",sitedesc);
            map.put("deptcode",deptcode);
            map.put("deptdesc",deptdesc);
            map.put("assetnum",assetnum);
            map.put("assetdesc",assetdesc);
            map.put("location",insaenum);
            map.put("locationdesc",insaedesc);
            map.put("createuserdesc", inspperdesc);
            map.put("createtime", createtime);
            map.put("updateuser",inspperdesc);

            String wotype = "1";// 1:故障工单
            map.put("wotype",wotype);
            // 生成工单编码
            Map<String, Object> woMap = new HashMap<>();
            handlerService.addBizNum("workorderApp","gq_workorder",woMap);
            // 查询工单编码
            String wonum = (String)woMap.get("wonum");
            map.put("wonum",wonum);
            //插入工单信息
            handlerService.insert( "gq_workorder", map);
            handlerService.updateBySql("update gq_workorder set createtime='"+createtime+"'where wonum ='" + wonum + "'" );
            json.put("msg", "生成工单成功, 请到工单管理界面查询数据!");
        }
        return json.toString();
    }

}
